#include "TicTacToe.h"
#include "Console.h"
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
using namespace Course;

TicTacToe::TicTacToe()
{
	clearBoard();
	xcursor = 0;
	ycursor = 0;
	turn = 1;
	score1 = 0;
	score2 = 0;
}


TicTacToe::~TicTacToe()
{
}

void TicTacToe::drawBoard()
{
	Console::clrscr();
	cout << endl << "   |   |   " << endl << "___|___|___" << endl
		<< "   |   |   " << endl << "___|___|___   " << endl << "   |   |   " << endl << "   |   |   " << endl;
	//cout << endl << "x: " << xcursor << " y:" << ycursor << " Turn: " << turn <<"   " << board[0][0]; //debugging
	for (int i = 0; i < 3; i++){
		for (int j = 0; j < 3; j++){
			Console::gotoxy(i * 3 + 2, j * 2 + 1);
			cout << board[i][j];
		}
	}
	Console::gotoxy(0, 8);
	cout << "Player1 score: " << score1 << "   Player2 score: " << score2;
	Console::gotoxy(xcursor * 3 + 2, ycursor * 2 + 1);
}
char TicTacToe::checkBoard()
{
	//check vertical and horisontal
	for (int i = 0; i < 3; i++){
		if (board[i][0] != ' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2]){
			return board[i][0];
		}
		else if (board[0][i] != ' ' && board[0][i] == board[1][i] && board[1][i] == board[2][i]){
			return board[0][0];
		}
	}
	//check diagonal
	if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2]){
		return board[0][0];
	}
	else if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] == board[2][0]){
		return board[0][2];
	}
	//check if room for more moves
	for (int i = 0; i < 3; i++){
		for (int j = 0; j < 3; j++){
			if (board[i][j] == ' '){
				return 't';
			}
		}
	}
	//no one won, no more moves possible, draw
	return 'd';
}

void TicTacToe::doTurn()
{
	while (true){
		int key = _getch();
		if (key == 75 && xcursor > 0){
			xcursor--;
		}
		else if (key == 72 && ycursor > 0){
			ycursor--;
		}
		else if (key == 77 && xcursor < 2){
			xcursor++;
		}
		else if (key == 80 && ycursor < 2){
			ycursor++;
		}
		else if (key == 13 && board[xcursor][ycursor] == ' '){
			if (turn == 1){
				board[xcursor][ycursor] = 'x';
				turn = 2;
			}
			else {
				board[xcursor][ycursor] = 'o';
				turn = 1;
			}
			drawBoard();
			break;
		}
		Console::gotoxy(xcursor * 3 + 2, ycursor * 2 + 1);
	}

}
void TicTacToe::clearBoard(){
	for (int i = 0; i < 3; i++){
		for (int j = 0; j < 3; j++){
			board[i][j] = ' ';
		}
	}
}

void TicTacToe::runGame()
{
	char result;
	while (true){
		drawBoard();
		while (true){
			doTurn();
			result = checkBoard();
			if (result != 't'){
				break;
			}
		}//game loop

		//end text
		string endMsg;
		if (result == 'x'){
			endMsg = "Player 1 wins!";
			score1++;
		}
		else if (result == 'o'){
			endMsg = "Player 2 wins!";
			score2++;
		}
		else{
			endMsg = "Draw!";
		}
		Console::gotoxy(0, 10);
		cout << endMsg << endl << endl;
		cout << "Press Return play again\n" << "Press any key to exit..\n";
		if (_getch() != 13){
			break;
		}
		clearBoard();
	}
}
